package com.wmd.core.conf;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Simple to Introduction
 *
 * @projectName:DocumentLibrary
 * @packageName:com.wmd.core.conf
 * @className:WebMvcConfig
 * @Description:[一句话描述该类的功能]
 * @Author:luyincheng
 * @CreateDate:2021/4/2 17:22
 * @blog:[方法具体上行/下行参数文档]
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer  {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");
    }
    @SuppressWarnings("unchecked")
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        try {
            Field registrationsField = FieldUtils.getField(InterceptorRegistry.class, "registrations", true);
            List<InterceptorRegistration> registrations = (List<InterceptorRegistration>) ReflectionUtils.getField(registrationsField, registry);
            if (registrations != null) {
                for (InterceptorRegistration interceptorRegistration : registrations) {
                    interceptorRegistration
                            .excludePathPatterns("/swagger**/**")
                            .excludePathPatterns("/webjars/**")
                            .excludePathPatterns("/v3/**")
                            .excludePathPatterns("/doc.html");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//               //.allowedOrigins("*")
//                .allowedOriginPatterns("*")
//                //.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
//                .allowedMethods("*")
//                .allowedHeaders("Origin","X-Requested-With","content-Type","Accept","Authorization","X-Authorization")
//                //.allowedHeaders("*")
//                .exposedHeaders("Origin","X-Requested-With","content-Type","Accept","Authorization","X-Authorization")
//                .maxAge(3600)
//                .allowCredentials(true);
//    }
//    private CorsConfiguration buildConfig() {
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        //corsConfiguration.addAllowedOrigin("*"); //允许任何域名
//        corsConfiguration.addAllowedHeader("Origin,X-Requested-With,content-Type,Accept,Authorization,X-Authorization"); //允许任何头
//        corsConfiguration.addAllowedMethod("*"); //允许任何方法
//        corsConfiguration.addAllowedOriginPattern("*");
//        corsConfiguration.setMaxAge(3600L);
//        corsConfiguration.setAllowCredentials(true);
//        corsConfiguration.addExposedHeader("Origin,X-Requested-With,content-Type,Accept,Authorization,X-Authorization");
//        return corsConfiguration;
//    }
//
//    @Bean
//    public CorsFilter corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", buildConfig()); //注册
//        return new CorsFilter(source);
//    }
//
//
//    @Bean
//    public FilterRegistrationBean<CorsFilter> corsFilter() {
//        log.info("跨域设置。。。。");
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        // 5 对接口配置跨域设置
//        source.registerCorsConfiguration("/**", buildConfig());
//        //有多个filter时此处设置改CorsFilter的优先执行顺序
//        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
//        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
//        return bean;
//    }


}